package com.yunhan.base;

import lombok.Setter;

import java.io.Serial;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 全局统一返回结果类
 *
 */
public class R<T> implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;
    private static final int FAIL_CODE = -1;
    private static final int SUCCESS_CODE = 200;

    //set
    //状态码
    @Setter
    private int code = SUCCESS_CODE;


    //承载数据
    @Setter
    private T data;

    //提示信息
    @Setter
    private String msg = "操作成功";

    private Map<String, List<String>> errColumn;


    //构造方法私有
    private R() {
    }

    private R(String msg) {
        this.msg = msg;
    }

    private R(T data) {
        this.data = data;
    }
    private R(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }
    private R(Integer code, Map<String, List<String>> errColumn) {
        this.code = code;
        this.msg = "参数异常";
        this.errColumn = errColumn;
    }

    private R(int code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
    }

    //操作成功相关方法-返回默认信息
    public static<T> R<T> ok(){
        return new R<>();
    }
    public static <T> R<T> ok(String msg){
        return new R<>(msg);
    }
    public static <T> R<T> ok(T data){
        return new R<>(data);
    }
    public static <T> R<T> ok(Integer code,String msg){
        return new R<>(code, msg);
    }
    public static <T> R<T> data(T data) {
        return new R<T>(SUCCESS_CODE, data, "操作成功");
    }
    //操作失败相关方法
    public static<T> R<T> fail(){
        return new R<>(FAIL_CODE, "操作失败");
    }
    public static<T> R<T> fail(String msg){
        return new R<>(FAIL_CODE, msg);
    }
    public static<T> R<T> fail(Integer code,String msg){
        return new R<>(code, msg);
    }
    public static<T> R<T> fail( Map<String, List<String>> errColumn){
        int ARGUMENT_ILLEGAL = 10001;
        return new R<>(ARGUMENT_ILLEGAL, errColumn);
    }

    //get
    public int getCode() {
        return this.code;
    }
    public T getData() {
        return this.data;
    }
    public String getMsg() {
        return this.msg;
    }
    public Map<String, List<String>> getErrColumn() {
        return this.errColumn;
    }

    public String toString() {
        return "R(code=" + this.getCode() + ", success=" + ", data=" + this.getData() + ", msg=" + this.getMsg() + ")";
    }



}
